<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\CateModel;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //分类首页
        $cateModel = new CateModel;
        $cates = $cateModel->getCates();

        return view('admin.cate.index',['cates' => $cates]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //分类添加页
        return view('admin.cate.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //保存
        $data = $request->except('_token');
        $cateModel = new CateModel;
        $res = $cateModel->insertCate($data);

        if ( $res ) {
            return ['msg' => '插入成功' , 'status' => 'success'];
        } else {
            return ['msg' => '插入失败' , 'status' => 'fail'];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //修改分类
        $cateModel = new CateModel;
        $cate = $cateModel->getCate($id);

        return view('admin.cate.edit',['cate' => $cate]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新
        $data = $request->except('_token','_method');
        $cateModel = new CateModel;
        $res = $cateModel->upCate($id , $data);

        if ( $res ) {
            return ['msg' => '更新成功' , 'status' => 'success'];
        } else {
            return ['msg' => '更新失败' , 'status' => 'fail'];
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除分类
        $cateModel = new CateModel;
        $res = $cateModel->delCate($id);

        if ( $res ) {
            return ['msg' => '删除成功' , 'status' => 'success'];
        } else {
            return ['msg' => '删除失败' , 'status' => 'fail'];
        }
    }
}
